package com.vf.admin.client.dao;

import com.vf.admin.common.db.BaseDao;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppCashWithdrawal;
import com.vf.admin.utils.StringEx;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 提现
 */
@Repository
public class AppCashWithdrawalDao extends BaseDao<AppCashWithdrawal> {

    public List<AppCashWithdrawal> page(Map<String, Object> paraMap, SplitPageBean splitPage) {
        ArrayList<Object> paramList = new ArrayList<>();
        StringBuilder sbStr = new StringBuilder("select * from app_cashwithdrawal where 1=1 ");
        if (StringEx.isNotNull(paraMap.get("userId"))) {
            sbStr.append(" and userid = ? ");
            paramList.add(paraMap.get("userId"));
        }
        sbStr.append(" order by createtime desc");
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppCashWithdrawal.class, splitPage);
    }

    public List<AppCashWithdrawal> findList(Integer userId, SplitPageBean splitPage) {
        ArrayList<Object> paramList = new ArrayList<>();
        StringBuilder sbStr = new StringBuilder("select * from app_cashwithdrawal where userid = ? ");
        paramList.add(userId);
        sbStr.append(" order by createtime desc");
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppCashWithdrawal.class, splitPage);
    }

    /**
     * 添加/修改
     */
    public void save(AppCashWithdrawal haoYou) {
        if (haoYou != null && haoYou.getId() != null && haoYou.getId() > 0) {
            this.update(haoYou);
        } else {
            haoYou.setId(this.add(haoYou));
        }
    }


    /**
     * 更新用户头像
     *
     * @param userid
     * @param pic    头像地址
     */
    public void updateImg(Integer userid, String pic) {
        if (StringEx.isNull(userid)) {
            return;
        }
        if (StringEx.isNull(pic)) {
            return;
        }
        String sql = " UPDATE app_cashwithdrawal SET pic = ?  WHERE userid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(pic);
        paramList.add(userid);
        this.update(sql, paramList);
    }

    /**
     * 更新用户昵称
     *
     * @param userid
     * @param nick   昵称
     */
    public void updateNick(Integer userid, String nick) {
        if (StringEx.isNull(userid)) {
            return;
        }
        if (StringEx.isNull(nick)) {
            return;
        }
        String sql = " UPDATE app_cashwithdrawal SET nick = ?  WHERE userid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(nick);
        paramList.add(userid);
        this.update(sql, paramList);
    }

    public void delByUserId(Integer userId) {
        String sql = "delete from app_cashwithdrawal where userid=" + userId;
        this.del(sql, new ArrayList<>());
    }

    public BigDecimal withdrawMoneySumByUserId(Integer userId) {
        ArrayList<Object> paramList = new ArrayList<>();
        StringBuilder sbStr = new StringBuilder("select IFNULL(sum(money), 0) as sumMoney from app_cashwithdrawal where userid = ? ");
        paramList.add(userId);
        String sql = sbStr.toString();
        Map<String, Object> map = this.queryObj(sql, paramList, Map.class);
        return new BigDecimal(map.get("sumMoney").toString());
    }
}
